
********* This program generates data with education and workplace network links *********

clear all
set more off, permanently
cd "$localdir\Data"
global output "$localdir\Output"


***** Education network links *****

use udda_final, clear

keep if year>=2008 & year<=2012

tab year

duplicates report pnr year // pnr + year is a unique observation

* Generate group ids by education, institution, and year of graduation
egen educ_id = group(gradyear educ instnr)

gen obs=1
sort gradyear educ instnr
bysort gradyear educ instnr: egen educ_n_all = sum(obs)
bysort gradyear educ instnr year: egen educ_n_year = sum(obs)

sum educ_id
sum educ_n_all
sum educ_n_year

* Save final data

keep pnr year educ_id educ instnr gradyear

save educid_20082012, replace







***** Workplace network links *****

use idan19802013, clear

tab aar

keep if aar>=2008

rename aar year

* Unique workplace id
rename lbnr firm

destring firm, replace
destring ansdage, replace

drop if firm==0 | firm==. // No workplace address
drop if ansdage==0 | ansdage==. // 0 days of employment

duplicates tag pnr firm year, gen(dup)
tab dup 
drop dup

g obs=1
bysort year firm: egen workers=total(obs)
drop obs
tabstat workers, by(year) s(mean med min max n)

keep pnr year firm workers

* Count total coworkers over time
* Define all people at a new work place as new coworkers
* Define number of coworkers in the same firm over time by the year with the largest number of workers

* Find year with most workers within a firm for each individual
bysort pnr firm: egen workers_max=max(workers)
bysort pnr firm: gen count=_n
bysort pnr count: egen temp=total(workers_max) if count==1
bysort pnr: egen workers_total=max(temp)
drop temp count

* Sum stats
g small_firm=0
replace small_firm=1 if workers<=25
bysort pnr: egen small_firm_max=max(small_firm) // At least one small firm

bysort pnr: gen count=_n

count if count==1
count if count==1 & small_firm_max==1 
count if count==1 & workers_total>=50 
count if count==1 & small_firm_max==1 & workers_total>=50 // Individuals
count if count==1 & small_firm_max==1 & workers_total<50 // Individuals

count if small_firm==1 & workers_total>=50 // Job relations
count if small_firm==1 & workers_total<50 // Job relations

sum workers_total if count==1, de

keep firm year pnr workers workers_total 

* Save final data 
save firmid_20082012, replace












